package dse.messages.singleversion;

import dse.*;
import dse.messages.*;

public class RequestShareV2MessageHandler extends MessageSender implements
		MessageHandler {
	private DataObject dobj;
	private int shareIndex;
	private ShareDownloader shareDownloader;
	//private LockProcessor lockProcessor;
	
	public RequestShareV2MessageHandler (
			Experiment experiment, 
			DataObject dobj,
			int shareIndex,
			ShareDownloader shareDownloader//,
			//LockProcessor processor
			)
	{
		super(experiment);
		
		this.dobj = dobj;
		this.shareIndex = shareIndex;
		this.shareDownloader = shareDownloader;
		//this.lockProcessor = processor;
	}
	
	@Override
	public boolean acceptMessage(Message message) {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public void messageInterupted(ExperimentRecorder recorder, Message message,
			double time) {
		// TODO Auto-generated method stub
		recorder.debug(this, "msgIntrpt: " + message, time);
		shareDownloader.shareDownloadFailed(
				recorder, dobj, shareIndex, time
				);
		if(message.from().isDown()){
			//this.lockProcessor.releaseLock(message.to(), recorder, time);
		}
	}

	@Override
	public void messageReceived(ExperimentRecorder recorder, Message message,
			double time) {
		// TODO Auto-generated method stub
		recorder.debug(this, "msgRcvd: " + message, time);
		if (message.messageType() != MessageType.RequestShareV2) {
			recorder.warn(
					"RequestShareV2MessageHandler recieved " + 
					message.messageType() + " message."
					);
			return;
		}			
		// What do we know about the share?
		Node receiver = message.to();
		//receiver.setSequence(Math.max(receiver.getSequence()+1, message.from().getSequence()));
		//receiver.sequence();
		Message m = super.createShareDataTransferMessage(
				receiver, message.from(), dobj, shareIndex, 
				message.purpose(), shareDownloader
				);
		//recorder.debug(this, "new msg: " + m, time);
		experiment.messageQueue().enqueueMessage(m, time);
		//this.lockProcessor.releaseLock(receiver, recorder, time);	

	}
}
